<template>
  <div>
    <v-md-editor
      :modelValue="modelValue"
      :height="height + 'px'"
      :disabled-menus="[]"
      :include-level="[1, 2, 3, 4, 5, 6]"
      @upload-image="handleUploadImage"
      @change="change"
    ></v-md-editor>
  </div>
</template>

<script setup>
import VMdEditor from "@kangc/v-md-editor";
import "@kangc/v-md-editor/lib/style/base-editor.css";
import githubTheme from "@kangc/v-md-editor/lib/theme/github.js";
import "@kangc/v-md-editor/lib/theme/style/github.css";

// highlightjs
import hljs from "highlight.js";
import { getCurrentInstance } from "vue";

const { proxy } = getCurrentInstance();

VMdEditor.use(githubTheme, {
  Hljs: hljs,
});

const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 1000,
  },
});

// 上传和加载图片
const handleUploadImage = async (event, insertImage, files) => {
  let result = await proxy.Request({
    url: "file/uploadImage",
    dataType: "file",
    params: {
      file: files[0],
      type: 1,
    },
  });
  if (!result) {
    return;
  }
  const url = proxy.globalInfo.imageUrl + result.data.fileName;
  console.log(url);
  insertImage({
    url: url,
    desc: "image",
    // width: 'auto',
    // height: 'auto',
  });
};

// 实现输入数据双向绑定
const emit = defineEmits();
const change = (markdownContent, htmlContent) => {
  emit("update:modelValue", markdownContent);
  emit("htmlContent", htmlContent);
};
</script>

<style lang="scss" scoped></style>
